/*
 * @Description: 游戏配置管理 - 自定义基线tab - 创建/编辑自定义基线表单弹窗
 * @Date: 2023-06-02 09:44:43
 * @Author: jiao-xiao-lin
 */
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { Modal, Form, Input } from 'antd';
import _ from 'lodash';
import { addBaseLine } from '@/api/game_managememnt';

const CreateModal = (props) => {
  const { visible, detail, changeModalShow } = props;

  const [form] = Form.useForm();

  // 调用创建接口
  const saveData = async (data) => {
    // 随机生成创建者id
    const insertUserId =
      'insertUserId-' + Math.random().toString(36).slice(0, 8);
    // 随机生成更新者id
    const updateUserId =
      'updateUserId-' + Math.random().toString(36).slice(0, 8);
    const resp = await addBaseLine({
      projectId: '614',
      name: data.name,
      insertUserId: insertUserId,
      insertUser: data.insertUser,
      updateUserId: updateUserId,
      updateUser: data.updateUser,
    });
    changeModalShow(false);
    props.click(resp);
  };

  const handleOk = () => {
    form.validateFields().then(
      (values) => {
        saveData(values);
      },
      () => {},
    );
  };

  const handleCancel = () => {
    changeModalShow(false);
  };

  useEffect(() => {
    if (!_.isEmpty(detail)) {
      form.setFieldValue(detail);
      form.setFieldsValue(detail);
    }
  }, [detail]);

  return (
    <Modal
      open={visible}
      title="创建自定义基线"
      cancelText="取消"
      okText="确定"
      onOk={handleOk}
      onCancel={handleCancel}
    >
      <Form form={form}>
        <Form.Item
          label="基线名称"
          name="name"
          rules={[
            {
              required: true,
              message: '请输入基线名称',
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="更新者"
          name="updateUser"
          rules={[
            {
              required: true,
              message: '更新者不能为空',
            },
          ]}
        >
          <Input />
        </Form.Item>
        <Form.Item
          label="创建者"
          name="insertUser"
          rules={[
            {
              required: true,
              message: '创建者不能为空',
            },
          ]}
        >
          <Input />
        </Form.Item>
      </Form>
    </Modal>
  );
};

CreateModal.prototype = {
  visible: PropTypes.bool,
  detail: PropTypes.object,
  changeModalShow: PropTypes.func,
};

export default CreateModal;
